package com.hourses.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hourses.mapper.PlaceMapper;
import com.hourses.pojo.Place;
import com.hourses.service.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/5/6 16:55
 */
@Service
public class PlaceServiceImpl implements PlaceService {
    //查询所有产地
    @Autowired
    private PlaceMapper placeMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public List<Place> selectAllPlaces() {
        //先看看有没有缓存
        String allPlaces = redisTemplate.opsForValue().get("allPlaces");
        if (StringUtils.hasText(allPlaces)){
            //有缓存
            return JSON.parseArray(allPlaces,Place.class);
        }
        //没有缓存
        //先查
        List<Place> places = placeMapper.selectAllPlaces();
        //再存入缓存
        redisTemplate.opsForValue().set("allPlaces",JSON.toJSONString(places));
        return places;
    }
}
